package com.zhuhjay.service.system.wxmp.handler;

import cn.hutool.core.util.StrUtil;
import com.zhuhjay.common.wxmp.builder.TextBuilder;
import com.zhuhjay.common.wxmp.handler.AbstractHandler;
import com.zhuhjay.service.system.wxmp.strategy.AbstractMessageStrategy;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author ZhuHJay
 * @date 2023/8/2 19:47
 */
@Slf4j
@Component
public class MessageHandler extends AbstractHandler {

    @Override
    public WxMpXmlOutMessage handle(
            WxMpXmlMessage wxMessage, Map<String, Object> context,
            WxMpService wxMpService, WxSessionManager sessionManager) {

        // 如果是文本
        if (wxMessage.getMsgType().equals(WxConsts.XmlMsgType.TEXT)) {
            String content = wxMessage.getContent();
            AbstractMessageStrategy handler = AbstractMessageStrategy.getHandler(content);
            if (handler != null) {
                return handler.handle(wxMessage, wxMpService);
            } else {
                return null;
            }
        }
        // 如果是图片
        else if (wxMessage.getMsgType().equals(WxConsts.XmlMsgType.IMAGE)) {
            // TODO 可以将图片上传到图床, 然后返回预览地址
            String message = "您发送的图片预览地址为 => {}";
            return new TextBuilder().build(StrUtil.format(message, wxMessage.getPicUrl()), wxMessage, wxMpService);
        }
        // 其他类型的消息
        else {
            return null;
        }
    }

}
